Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        var latitude = res.latitude
        var longitude = res.longitude

        console.log(latitude)
        console.log(longitude)
        that.setData({
          latitude: latitude,
          longitude: longitude
        })

        wx.request({
          url: 'https://apis.map.qq.com/ws/place/v1/search?key=Z4DBZ-A4FLO-W72W6-S3IJJ-V3EJ5-QEFPD&keyword=加油站&boundary=nearby(' + latitude + ',' + longitude + ',1000)&page_size=20',
          success: function(res) {
            
            var markers = [] //地图markers标记点
            var address = [] //地址数据
            var arr = res.data.data
            console.log(res)
            for (var i = 0; i < arr.length; i++) {
              markers.push({
                iconPath: "https://codermoyv.gitee.io/coder-moyv/assets/images/wechat/e_life_helper/icon_oli.png",
                id: i,
                latitude: arr[i].location.lat,
                longitude: arr[i].location.lng,
                width: 32,
                height: 32
              })
              address.push({
                title: arr[i].title,
                address: arr[i].address
              })
            }
            markers[0].iconPath ="https://codermoyv.gitee.io/coder-moyv/assets/images/wechat/e_life_helper/icon_oli_active.png"//将第一位设置为选中
            that.setData({
              markers: markers,
              address: address
            })
            
          }
        })

      },
    })
  },
  /**
   * markers点击事件
   */
  markertap:function(e){
    var id = e.detail.markerId
    var markers = this.data.markers
    for (var i = 0; i < markers.length; i++) {
      if (markers[i].id == id) {
        markers[i].iconPath = "https://codermoyv.gitee.io/coder-moyv/assets/images/wechat/e_life_helper/icon_oli_active.png"
      } else {
        markers[i].iconPath = "https://codermoyv.gitee.io/coder-moyv/assets/images/wechat/e_life_helper/icon_oli.png"
      }
    }

    this.setData({
      markers: markers,
      index: id
    })
  },
  /**
   * 导航按钮
   */
  nav: function (e) {
    let plugin = requirePlugin('routePlan');
    let key = 'Z4DBZ-A4FLO-W72W6-S3IJJ-V3EJ5-QEFPD';  //使用在腾讯位置服务申请的key
    let referer = '油价';   //调用插件的app的名称
    let endPoint = JSON.stringify({  //终点
      'name': this.data.address[this.data.index].title,
      'latitude': this.data.markers[this.data.index].latitude,
      'longitude': this.data.markers[this.data.index].longitude
    });
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
    });
  }
})
